﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using UpperComputer.WPF.Entry.Views;
using YFurion.Application;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class MenuButtonViewModel : ManagementViewModel
    {
        private readonly IBaseMenuButtonService _menuButtonService;

        public BaseMenuButtonResultRequestDto Query { get; } = new BaseMenuButtonResultRequestDto();

        private PagedResultDto<BaseMenuButtonResultDto> _buttonPaged = new PagedResultDto<BaseMenuButtonResultDto>();
        public PagedResultDto<BaseMenuButtonResultDto> ButtonPaged
        {
            get => _buttonPaged;
            set => SetProperty(ref _buttonPaged, value);
        }

        private BaseMenuButtonResultDto _currentMenuButton;
        public BaseMenuButtonResultDto CurrentMenuButton
        {
            get => _currentMenuButton;
            set => SetProperty(ref _currentMenuButton, value);
        }

        public MenuButtonViewModel(IBaseMenuButtonService menuButtonService)
        {
            _menuButtonService = menuButtonService;
        }

        protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
        {
            SetCurrent(parameter);
            SetSkipCount(Query);
            ButtonPaged = await _menuButtonService.GetCustomAll(Query, cancellationToken);
            CurrentMenuButton = null;
        }

        protected override async Task PostAsync(CancellationToken cancellationToken)
        {
            MenuButtonOperationControl menuButtonOperationControl = Furion.App.GetService<MenuButtonOperationControl>();
            object result = await ShowRootDialog(menuButtonOperationControl);
            if (result != null)
            {
                await LoadAsync(1, cancellationToken);
            }
        }

        protected override async Task PutAsync(CancellationToken cancellationToken)
        {
            if (CurrentMenuButton == null)
            {
                Snackbar.ShowWarningMessage("请选择一行编辑");
                return;
            }
            MenuButtonOperationControl menuButtonOperationControl = Native.CreateInstance<MenuButtonOperationControl>(CurrentMenuButton.Id);
            object result = await ShowRootDialog(menuButtonOperationControl);
            if (result != null)
            {
                await LoadAsync(null, cancellationToken);
            }
        }

        protected override async Task DeleteAsync(CancellationToken cancellationToken)
        {
            if (CurrentMenuButton == null)
            {
                Snackbar.ShowWarningMessage("请选择一行删除");
                return;
            }
            MessageBoxResult mbr = this.ShowQuestionBox("确定要删除吗");
            if (mbr == MessageBoxResult.OK)
            {
                await _menuButtonService.Delete(CurrentMenuButton.Id, cancellationToken);
            }
        }

        protected override async Task OperationAsync(string operation, CancellationToken cancellationToken)
        {
            MenuButtonCopyControl menuButtonCopyControl = Furion.App.GetService<MenuButtonCopyControl>();
            object result = await ShowRootDialog(menuButtonCopyControl);
            if (result != null)
            {
                await LoadAsync(1, cancellationToken);
            }
        }
    }
}
